<?php
namespace app\index\controller;

use think\Model;  
class Index extends Common
{
	//网站首页
    public function index()
    {
        $fwl=$this->fwl();
        //logo
        $logo=db('logo')->where('caro_states',1)->column('logo_img');
        $this->assign('logo',$logo[0]);
        //分类导航
        $typedata=db('types')->where('type_pid',1)->select();
        foreach($typedata as $k=>$v)
        {
            $v['typeson']=db('types')->where('type_pid',$v['type_id'])->select();
            $typedata[$k]=$v;
        }
        //halt($typedata);
        $this->assign('typedata',$typedata);
        //轮播图
        $imgdata=db('carousel')->where('caro_states',1)->field('caro_img')->select();
        //halt($imgdata);
        $this->assign('imgdata',$imgdata);
        //关于我们
        //我们做什么
        //产品展示
        $protype=db('protypes')->where('protype_states',1)->where('protype_pid',1)->select();
         foreach($protype as $k=>$v)
        {
            $v['proson']=db('protypes')->where('protype_pid',$v['protype_id'])->select();
            $protype[$k]=$v;
        }
        //获得第一个分类的产品
          $dataid=db('protypes')->where('protype_pid',1)->where('protype_states',1)->field('protype_id')->find();
          $type_pid=$dataid['protype_id'];
                $res=$this->art_son($type_pid);
                //halt($res);
                $proson=db('products')->where('prod_states',1)->whereIn('protype_pid',$res)->select();
        //halt($protype);
        //$products=db('products')->where('prod_states',1)->select();
        //halt($protype);
        $this->assign('protype',$protype);
         $this->assign('proson',$proson);
        //新闻展示id10;
        $news=db('types')->where('type_pid',10)->where('type_states',1)->select();
        foreach($news as $k=>$v)
        {
            $v['show']=db('contents')->where('cont_pid',$v['type_id'])->where('cont_states',2)->find();

            $v['shows']=db('contents')->where('cont_pid',$v['type_id'])->where('cont_states',1)->limit(4)->select();
            $news[$k]=$v;

        }
        //halt($news);
        $this->assign('news',$news);
        return $this->fetch();
    }
      //访问记录
    public function fwl()
    {
        $Ip=getIP();
        $time=time();
    $date['btime']=$time;
    $y_Id=db("fwl")->where("ip='{$Ip}'")->column('fid');
    //return $y_Id;
        if(!empty($y_Id))
        {
            $y_count=db("fwl")->where("ip='{$Ip}'")->column("count");
        
            $y_time=db("fwl")->where("ip='{$Ip}'")->column("btime");
            //halt($y_time);
            $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
        
            $endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
            if($y_time[0]>$beginToday && $y_time[0]<$endToday)
            {
                $date['count']=$y_count[0]+1;
                $date['btime']=$time;
                $add_IP=db("fwl")->where("ip='{$Ip}'")->update(['count'=>$y_count[0]+1,'btime'=>$time]);
            }else
            {
                $date['ip']=$Ip;
                $date['btime']=$time;
                $date['count']=1;
                $add_IP=db("fwl")->insert($date);
            }
        }else
        {
            $date['ip']=$Ip;
            $date['count']=1;
            $add_IP=db("fwl")->insert($date);
        }
    }
     /**
    * 获得自己和自己的子集
    */
   public function art_son($type_id)
   {
        $allData=db('protypes')->select();
       //获得自己和自己子集的cate_id
       $sonDate=$this->getSonId($allData,$type_id);
       //把自己id加进去
       $sonDate[]=$type_id;
       return $sonDate;
   }
    
   /**
    * 获得子集type_id
    */
   public function getSonId($allData,$type_id)
   {
        static $tmp=array();
        foreach($allData as $k=>$v)
        {
           if($type_id==$v['protype_pid']){
             $tmp[]=$v['protype_id'];
             $this->getSonId($allData,$v['protype_id']);
           }
          
        }
        return $tmp;
    }
}
